//
//  YTTools.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/13.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTTools.h"
#import "Reachability.h"
#import <CommonCrypto/CommonDigest.h>

@implementation YTTools

+(dispatch_source_t)gcdTimer:(CGFloat)time
{
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 设置定时器的各种属性（几时开始任务，每隔多长时间执行一次）
    // GCD的时间参数，一般是纳秒（1秒 == 10的9次方纳秒）
    // 何时开始执行第一个任务
    // dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC) 比当前时间晚3秒
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC));
    uint64_t interval = (uint64_t)(time * NSEC_PER_SEC);
    dispatch_source_set_timer(timer,  start, interval, 0);
    return timer;
}

+(dispatch_source_t)gcdTimer:(CGFloat)time DelayTime:(CGFloat)delayTime
{
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 设置定时器的各种属性（几时开始任务，每隔多长时间执行一次）
    // GCD的时间参数，一般是纳秒（1秒 == 10的9次方纳秒）
    // 何时开始执行第一个任务
    // dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC) 比当前时间晚3秒
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC));
    uint64_t interval = (uint64_t)(time * NSEC_PER_SEC);
    dispatch_source_set_timer(timer,  start, interval, 0);
    return timer;
}
+(NSString *)getHHMMSSFromSS:(NSInteger)totalTime
{
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",totalTime/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(totalTime%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",totalTime%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    
    return format_time;
    
}
+(void)gotoWifi
{
    if ([SYSTEM_VERSION doubleValue] >= 10.0)
    {
        NSURL *url = [NSURL URLWithString:@"App-Prefs:root=WIFI"];
        if (![SharedApplication canOpenURL:url]){return;}
        [SharedApplication openURL:url options:@{} completionHandler:nil];
    }else
    {
        NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"];
        if (![SharedApplication canOpenURL:url]){return;}
        [SharedApplication openURL:url];
    }
}
+(void)gotoBluetooth
{
    if ([SYSTEM_VERSION doubleValue] >= 10.0)
    {
        NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Bluetooth"];
        if (![SharedApplication canOpenURL:url]){return;}
        [SharedApplication openURL:url options:@{} completionHandler:nil];
    }else
    {
        NSURL *url = [NSURL URLWithString:@"prefs:root=Bluetooth"];
        if (![SharedApplication canOpenURL:url]){return;}
        [SharedApplication openURL:url];
    }
}
+(int)getNetState{
    /**
     status 0 没有网络
     status 1 wifi网络
     status 2 4g蜂窝网络
     */
    Reachability *reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
    int status = [reachability currentReachabilityStatus];
    return status;
}
+ (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
@end
